package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsCouponUser;
import com.qf.service.DtsCouponService;
import com.qf.service.DtsCouponUserService;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Cc
 * @version 1.0
 * 优惠券管理
 */
@RestController
@CrossOrigin("*")
@RequestMapping("admin/coupon")
public class AdminCouponController {


    @Autowired
    private DtsCouponService dtsCouponService;
    @Autowired
    private DtsCouponUserService dtsCouponUserService;


    /**
     * 查询优惠券列表
     * 接口地址 : http://localhost:8083/admin/coupon/list?page=1&limit=20&sort=add_time&order=desc
     *
     * @param username
     * @param sort
     * @param order
     * @param page
     * @param limit
     * @return
     */
    @RequiresPermissions("admin:coupon:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String username, String sort, String order,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit) {

        IPage<DtsCoupon> storageIPage = dtsCouponService.page(username, page, limit, sort, order);

        HashMap map = new HashMap();
        map.put("total", storageIPage.getTotal());
        map.put("items", storageIPage.getRecords());
        return ResponseUtil.ok(map);
    }

    /**
     * 优惠券详情
     * 接口地址 : http://localhost:8083/admin/coupon/read?id=11
     *
     * @param id
     * @return
     */
    @RequiresPermissions("admin:coupon:read")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(@RequestParam("id") Integer id) {
        DtsCoupon dtsCoupon = dtsCouponService.findById(id);

        return ResponseUtil.ok(dtsCoupon);
    }

    /**
     * 此优惠劵使用的用户列表 :
     * 接口地址 : http://localhost:8083/admin/coupon/listuser?page=1&limit=20&couponId=11&sort=add_time&order=desc
     *
     * @param couponId
     * @param sort
     * @param order
     * @param page
     * @param limit
     * @return
     */
    @RequiresPermissions("admin:coupon:listuser")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "优惠劵使用的用户列表")
    @GetMapping("/listuser")
    public Object listuser(Integer couponId, String sort, String order,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "20") Integer limit) {
        IPage<DtsCouponUser> storageIPage = dtsCouponUserService.page(couponId, sort, order, page, limit);
        HashMap map = new HashMap();
        map.put("total", storageIPage.getTotal());
        map.put("items", storageIPage.getRecords());
        return ResponseUtil.ok(map);
    }

    /**
     * 优惠券添加
     *
     * @param body
     * @return
     * @throws ParseException
     */
    @RequiresPermissions("admin:coupon:create")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody String body) throws ParseException {
        DtsCoupon coupon = new DtsCoupon();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        coupon.setName(JacksonUtil.parseString(body, "name"))
                .setTotal(JacksonUtil.parseInteger(body, "total"))
                .setDiscount(new BigDecimal(JacksonUtil.parseInteger(body, "discount")))
                .setMin(new BigDecimal(JacksonUtil.parseInteger(body, "min")))
                .setLimit(JacksonUtil.parseInteger(body, "limit"))
                .setType(JacksonUtil.parseInteger(body, "type"))
                .setStatus(JacksonUtil.parseInteger(body, "status"))
                .setGoodsType(JacksonUtil.parseInteger(body, "goodsType"))
                .setGoodsValue(JacksonUtil.parseString(body, "goodsValue"))
                .setTimeType(JacksonUtil.parseInteger(body, "timeType"))
                .setDays(JacksonUtil.parseInteger(body, "days"))
                .setDesc(JacksonUtil.parseString(body, "desc"))
                .setTag(JacksonUtil.parseString(body, "tag"));
        if (JacksonUtil.parseString(body, "startTime").isEmpty()) {
            coupon.setStartTime(format.parse(JacksonUtil.parseString(body, "startTime")));
        }
        if (JacksonUtil.parseString(body, "endTime").isEmpty()) {
            coupon.setEndTime(format.parse(JacksonUtil.parseString(body, "endTime")));
        }
        int i = dtsCouponService.add(coupon);
        return ResponseUtil.ok(coupon);
    }

    /**
     * 优惠券修改
     * <p>
     * 接口地址 : http://localhost:8083/admin/coupon/update
     */
    @RequiresPermissions("admin:coupon:update")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "修改")
    @PostMapping("/update")
    public Object update(@RequestBody DtsCoupon coupon) {
        Integer id = coupon.getId();
        String name = coupon.getName();
        String desc = coupon.getDesc();
        String tag = coupon.getTag();
        Integer total = coupon.getTotal();
        BigDecimal discount = coupon.getDiscount();
        BigDecimal min = coupon.getMin();
        Integer limit = coupon.getLimit();
        Integer type = coupon.getType();
        Integer status = coupon.getStatus();
        Integer goodsType = coupon.getGoodsType();
        String goodsValue = coupon.getGoodsValue();
        String code = coupon.getCode();
        Integer timeType = coupon.getTimeType();
        Integer days = coupon.getDays();
        Date startTime = coupon.getStartTime();
        Date endTime = coupon.getEndTime();
        Date addTime = coupon.getAddTime();
        Date updateTime = coupon.getUpdateTime();
        Boolean deleted = coupon.getDeleted();

        Map<String, Object> map = new HashMap();
        DtsCoupon dtsCoupon = new DtsCoupon();
        dtsCoupon.setId(id);
        dtsCoupon.setName(name);
        dtsCoupon.setDesc(desc);
        dtsCoupon.setTag(tag);
        dtsCoupon.setTotal(total);
        dtsCoupon.setDiscount(discount);
        dtsCoupon.setMin(min);
        dtsCoupon.setLimit(limit);
        dtsCoupon.setType(type);
        dtsCoupon.setStatus(status);
        dtsCoupon.setGoodsType(goodsType);
        dtsCoupon.setGoodsValue(goodsValue);
        dtsCoupon.setCode(code);
        dtsCoupon.setTimeType(timeType);
        dtsCoupon.setDays(days);
        dtsCoupon.setStartTime(startTime);
        dtsCoupon.setEndTime(endTime);
        dtsCoupon.setAddTime(addTime);
        dtsCoupon.setUpdateTime(updateTime);
        dtsCoupon.setDeleted(deleted);
        map = dtsCouponService.update(dtsCoupon);

        return ResponseUtil.ok(map);

    }

    /**
     * 接口地址 : http://localhost:8083/admin/coupon/delete
     * @return
     */
    @RequiresPermissions("admin:coupon:delete")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsCoupon coupon){
        Integer id = coupon.getId();
        dtsCouponService.delete(id);
        return ResponseUtil.ok();
    }

}
